package com.glsc.ngateway.platform.service.it;

import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.ItApplicationDetail;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.ItOperation;
import com.glsc.ngateway.common.api.platform.dto.it.ParamSearchAppDto;
import com.glsc.ngateway.common.api.platform.dto.it.ParamSearchSystemDto;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItApplicationDetailRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItOperationRepo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OperationService {

    Logger logger = LoggerFactory.getLogger(OperationService.class);

    @Autowired
    private ItApplicationDetailRepo itApplicationDetailRepo;
    @Autowired
    private ItOperationRepo itOperationRepo;

    @Autowired
    private SequenceService sequenceService;

    public Page<ItOperation> queryOperationByUserId(Integer page, Integer size, String userId, ParamSearchSystemDto key) {
//        String sql = "select a.op_id,a.op_name,a.op_style,a.op_build,a.op_support,a.op_user,a.status,b.user_name " +
//                "from t_operation a,t_admin_user b where a.op_user=b.user_id and b.user_id=? order by a.insert_time desc";
//        return BeanUtil.underline2Camel(jdbcTemplate.queryForList(sql, new Object[]{userId}));
        Pageable pageable = PageRequest.of(page - 1, size);
        Page<ItOperation> list = itOperationRepo.getByOpUser(pageable, userId,key);
        return list;
    }
//    查询全部应用明细列表
    public List<ItApplicationDetail> queryAllAppList(){
        List<ItApplicationDetail> list = itApplicationDetailRepo.findAll();
        return list;
    }


    //查询应用分类列表(无权限用户控制)
    public Page<ItApplicationDetail> queryAppByMxId(Integer page, Integer size, ParamSearchAppDto params) {
        Pageable pageable = PageRequest.of(page - 1, size);
        Page<ItApplicationDetail> list = itApplicationDetailRepo.query(pageable,params);
        return list;
    }

    public void deleteShowmx(String mxId) {
        ItApplicationDetail it = itApplicationDetailRepo.findByMxId(mxId);
        if (it != null) {
            itApplicationDetailRepo.delete(it);
        }
    }

    public void addOrUpdateShowmx(ItApplicationDetail itApplicationDetail, String userId) {

        if (StringUtils.isEmpty(itApplicationDetail.getMxId())) {
            itApplicationDetail.setMxId("MX-" + sequenceService.getNextValue("show_mx"));
            itApplicationDetail.setUserId(userId);
        }
        itApplicationDetailRepo.save(itApplicationDetail);
    }

    public void addOrUpdateOperation(ItOperation itOperation, String userId) {

        if (StringUtils.isEmpty(itOperation.getOpId())) {
            itOperation.setOpId("OP-" + sequenceService.getNextValue("run_operation"));
            itOperation.setOpUser(userId);
        }
        itOperationRepo.save(itOperation);
    }

    public void deleteOperation(String opId) {
        ItOperation it = itOperationRepo.findByOpId(opId);
        if (it != null) {
            itOperationRepo.delete(it);
        }
    }


    public List<ItOperation> queryAllOperation() {
        return itOperationRepo.findAll();
    }

//    public List<Map<String, Object>> queryAllRun() {
//        String sql = "select * from t_operation order by insert_time";
//        return BeanUtil.underline2Camel(jdbcTemplate.queryForList(sql));
//    }
//
//    //查询系统分类列表
//    public List<Map<String, Object>> querySystemListByOpId() {
//        String sql = "select op_id,op_name from t_operation";
//        return BeanUtil.underline2Camel(jdbcTemplate.queryForList(sql));
//    }
//

//
//    //查询应用系统ID
//    public List<Map<String, Object>> queryOpidByOpUser(String opUser) {
//        String sql = "select op_id,op_name from t_operation where op_user=?";
////		List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(sql, new Object[] {opUser});   测试看是否有数据
//        return BeanUtil.underline2Camel(jdbcTemplate.queryForList(sql, new Object[]{opUser}));
//    }
//
//    ///////////////////////////////////////
//    //查询详情页面
//    public List<Map<String, Object>> queryOpViewByOpId(String opId) {
//        String sql = "select a.op_name,b.as_shape,b.as_area,b.as_type,b.as_brand,b.as_model,b.as_ip,b.as_os,c.mx_name " +
//                " from t_operation a " +
//                " join t_application_sys b on a.op_id = b.op_id " +
//                " join t_yinyong_detail c on a.op_id = c.op_id and b.op_id=?";
////		List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(sql, new Object[] {opUser});   测试看是否有数据
//        return BeanUtil.underline2Camel(jdbcTemplate.queryForList(sql, new Object[]{opId}));
//    }
//
////////////////////////////////////////
//
//    //查询应用明细列表(根据用户名)
//    public List<Map<String, Object>> queryShowByUserId(String userId) {
//        String sql = "select * from t_yinyong_detail where user_id=?";
//        return BeanUtil.underline2Camel(jdbcTemplate.queryForList(sql, new Object[]{userId}));
//    }

//    public Map<String, Object> getInfo(String opId) {
//        Map<String, Object> conditions = new HashMap<String, Object>();
//        conditions.put("op_id", opId);
//        return BeanUtil.underline2Camel(generalDataManager.queryForMap("t_operation", conditions));
//    }

}
